import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
  // 需要认证的路径
  const protectedPaths = ['/dashboard', '/projects', '/todos']
  
  // 检查当前路径是否需要认证
  const isProtectedPath = protectedPaths.some(path => 
    request.nextUrl.pathname.startsWith(path)
  )

  if (isProtectedPath) {
    // 检查是否有用户ID cookie
    const userId = request.cookies.get('user_id')
    const accessToken = request.cookies.get('access_token')

    if (!userId || !accessToken) {
      // 重定向到首页
      return NextResponse.redirect(new URL('/', request.url))
    }
  }

  return NextResponse.next()
}

export const config = {
  matcher: [
    /*
     * 匹配所有请求路径，除了以下开头的路径：
     * - api (API routes)
     * - _next/static (static files)
     * - _next/image (image optimization files)
     * - favicon.ico (favicon file)
     */
    '/((?!api|_next/static|_next/image|favicon.ico).*)',
  ],
} 